/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package digitaldreamers.games.ddmonsters.servers.gameserver.gameserverui;

import digitaldreamers.libraries.games.servers.gameserver.ICommonGameServer;
import digitaldreamers.libraries.games.servers.gameserver.IGameServerUI;
import digitaldreamers.games.ddmonsters.servers.gameserver.configurations.Configuration;
import digitaldreamers.games.ddmonsters.servers.gameserver.DDMonsterGameServer;
import java.io.IOException;

/**
 *
 * @author DaoLe
 */
public class PaperMonsterServerManagementPanel extends javax.swing.JFrame implements IGameServerUI {
    private static final String NEW_LINE = System.getProperty("line.separator");
    
    private ICommonGameServer mGameServer;
    private boolean mIsStarted;
    
    /**
     * Creates new form PaperMonsterServerManagementPanel
     */
    public PaperMonsterServerManagementPanel() {
        initComponents();
        mIsStarted = false;
        btnStopServer.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        btnStartServer = new javax.swing.JButton();
        btnStopServer = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        btnStartServer.setText("Start Server");
        btnStartServer.setName("btnStartServer"); // NOI18N
        btnStartServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartServerActionPerformed(evt);
            }
        });

        btnStopServer.setText("Stop Server");
        btnStopServer.setName("btnStopServer"); // NOI18N
        btnStopServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopServerActionPerformed(evt);
            }
        });

        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        txtMessage.setEnabled(false);
        txtMessage.setName("txtMessage"); // NOI18N
        txtMessage.setPreferredSize(null);
        jScrollPane2.setViewportView(txtMessage);
        txtMessage.getAccessibleContext().setAccessibleName("txtMessage");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnStartServer)
                        .addGap(18, 18, 18)
                        .addComponent(btnStopServer))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 527, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnStartServer)
                    .addComponent(btnStopServer))
                .addContainerGap())
        );

        btnStartServer.getAccessibleContext().setAccessibleName("btnStartServer");
        btnStopServer.getAccessibleContext().setAccessibleName("btnStopServer");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartServerActionPerformed
        // TODO add your handling code here:
        start();
    }//GEN-LAST:event_btnStartServerActionPerformed

    private void btnStopServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopServerActionPerformed
        // TODO add your handling code here:
        stop();
    }//GEN-LAST:event_btnStopServerActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        stop();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PaperMonsterServerManagementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PaperMonsterServerManagementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PaperMonsterServerManagementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PaperMonsterServerManagementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PaperMonsterServerManagementPanel().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStartServer;
    private javax.swing.JButton btnStopServer;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea txtMessage;
    // End of variables declaration//GEN-END:variables
    
    private void start() {
        try {
            if(!mIsStarted) {
                if (mGameServer == null) {
                    mGameServer = new DDMonsterGameServer(this, Configuration.SERVER_PORT);
                }
                mGameServer.start();
                btnStartServer.setEnabled(false);
                btnStopServer.setEnabled(true);
                mIsStarted = true;
            }
        } catch (IOException ex) {
            showMessage(ex.getMessage());
        }
    }
    
    private void stop() {
        try {
            if(mIsStarted) {
                mGameServer.stop();
                btnStartServer.setEnabled(true);
                btnStopServer.setEnabled(false);
                mIsStarted = false;
            }
        } catch (InterruptedException ex) {
            showMessage(ex.getMessage());
        } catch (IOException ex) {
            showMessage(ex.getMessage());
        }
    }
    
    /**
     * Show message on the UI.
     * @param strMessage The message to show on the UI.
     */
    @Override
    public void showMessage(String strMessage) {
        txtMessage.append(strMessage + NEW_LINE);
    }
}
